//
//  MainViewController_iPhone.m
//  PhongThuyHuongNha
//
//  Created by Le Anh Tai on 9/28/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "MainViewController_iPhone.h"
#import "FlatDatePicker.h"
#import "UIConstants.h"
#import "Utility.h"
#import "HeaderSectionResultView.h"
#import "HuongInfoCell.h"
#import "OwnerInfoCalloutView_iPhone.h"

#define SECTION_HUONG_TOT 0
#define SECTION_HUONG_XAU 1

@interface MainViewController_iPhone ()

@end

@implementation MainViewController_iPhone

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self)
    {
        // Custom initialization
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableView.allowsSelection = NO;
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [_explainationView addSubview:_tableView];
    
    //footer result view
    _ownerInfoFooterView = [[FooterResultView alloc] init];
    _ownerInfoFooterView.delegate = self;
    
    //copy right
    self.footerLabel.font = FONT_HELVETICANEUE(13);
}

- (OwnerInfoCalloutView *)newOwnerInfoCalloutView
{
    return [[OwnerInfoCalloutView_iPhone alloc] init];
}

- (void)showResult
{
    [super showResult];
    [_ownerInfoCallout close];    
    
    [_tableView setContentOffset:CGPointZero animated:YES];
    
    NSMutableString *ownerInfo = [[NSMutableString alloc] init];
    
    [ownerInfo appendFormat:@"%@, ",[_fengShuiCore genderString]];
    _homeDirection.phiCung.cung = [_fengShuiCore getPhiCung];
    
    if (_ownerInfoCallout.isAmLich)
    {
        [ownerInfo appendFormat:@"sinh ngày %@ AL, năm %@ %@, bản mệnh: %@.\n",[[_fengShuiCore lunarDate] stringddmmyyyy],[_fengShuiCore getCan], [_fengShuiCore getChi],[_fengShuiCore getMang]];
    }
    else
    {
        [ownerInfo appendFormat:@"sinh ngày %@ nhằm %@ AL, năm %@ %@, bản mệnh: %@.\n",[_ownerInfoCallout.birthday stringddmmyyyy],[_fengShuiCore.lunarDate stringddmmyyyy ],[_fengShuiCore getCan], [_fengShuiCore getChi],[_fengShuiCore getMang]];
    }
    
    [ownerInfo appendFormat:@"Phi Cung %@, thuộc %@ tứ trạch.\n",[_fengShuiCore getPhiCung],[_homeDirection.phiCung getThuocTuTrach]];
    
    [_ownerInfoHeaderView loadOwnerInfo:ownerInfo];
    _ownerInfoHeaderView.frame = _tableView.bounds;
    
    if (_huongInfos == nil)
    {
        _huongInfos = [[NSMutableArray alloc] init];
    }
    
    [_huongInfos removeAllObjects];
    
    NSArray *huongTot = [_homeDirection get4HuongTot];
    NSArray *huongXau = [_homeDirection get4HuongXau];
    
    if (huongTot)
    {
        [_huongInfos addObject:huongTot];
    }
    
    if (huongXau)
    {
        [_huongInfos addObject:huongXau];
    }
    
    //
    NSString *batQuaiImageName = @"";
    NSInteger quaiSoIdx = _homeDirection.phiCung.index;
    if (quaiSoIdx > -1)
    {
        batQuaiImageName = [NSString stringWithFormat:@"batquai_%d.png", quaiSoIdx+1];
    }
    
    NSLog(@"Bat quai image: %@", batQuaiImageName);
    [self updateBatQuaiImage:batQuaiImageName];
    
    [_tableView setTableHeaderView:_ownerInfoHeaderView];
    [_tableView setTableFooterView:_ownerInfoFooterView];
    [_tableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

#pragma mark - ownerInfo delegate

//iphone
-(void)OwnerInfoCalloutViewDidPressedExplainationButton:(OwnerInfoCalloutView *)view
{
    [self showResult];
    
    [self infoButtonPressed:nil];
}

//iphone
-(void)OwnerInfoCalloutViewDidPressedBatTrachButton:(OwnerInfoCalloutView *)view
{
    [self showResult];
}

#pragma mark - Delegate UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_huongInfos == nil)
    {
        return 0;
    }
    
    return _huongInfos.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderSectionResultView *header = [[HeaderSectionResultView alloc] init];
    
    if (section == SECTION_HUONG_TOT)
    {
        [header loadHeaderText:@"Bốn Hướng Tốt"];
    }
    else
    {
        [header loadHeaderText:@"Bốn Hướng Xấu"];
    }
    
    return header;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *huongs = [Utility getObject:_huongInfos atIndex:section];
    
    if (huongs == nil)
    {
        return 0;
    }
    
    return huongs.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    
    HuongInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil)
    {
        cell = [[HuongInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSArray *huong = [Utility getObject:_huongInfos atIndex:indexPath.section];
    if (huong)
    {
        [cell loadInfo:[Utility getObject:huong atIndex:indexPath.row]];
    }
    
    if (indexPath.section == SECTION_HUONG_TOT)
    {
        [cell setTextColor:COLOR_BLUE_IOS7];
    }
    else if (indexPath.section == SECTION_HUONG_XAU)
    {
        [cell setTextColor:COLOR_LIGHT_BLACK];
    }
    
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload
{
    [self setBirthdayButton:nil];
    [super viewDidUnload];
}
@end
